/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.bean.LicenseInfo;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.BusinessBillItem;
import com.minierp.model.Establishment;
import com.minierp.model.EstablishmentContact;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class DeliverBillListForAccountExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"DeliverBillListForAccountPrint.xls\"");
		HSSFSheet sheet = workbook.createSheet("list");
		LicenseInfo lif = EncCommonHelper.getLicenseInfo(request);
		List list = (List) model.get("list");
		List mtList = (List) model.get("mtList");
		Establishment est = (Establishment) model.get("est");
		String includeSub = (String) model.get("include_sub");
		if (includeSub == null) {
			includeSub = "";
		}

		String startDate = (String) model.get("start_date");
		if (startDate == null) {
			startDate = "";
		}

		String endDate = (String) model.get("end_date");
		if (endDate == null) {
			endDate = "";
		}

		HSSFFont titltFont = workbook.createFont();
		titltFont.setFontHeightInPoints(16);
		titltFont.setBoldweight(700);
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		HSSFFont normalFont = workbook.createFont();
		HSSFCellStyle titleCellStyle = workbook.createCellStyle();
		titleCellStyle.setFont(titltFont);
		titleCellStyle.setAlignment(2);
		titleCellStyle.setVerticalAlignment(1);
		HSSFCellStyle norStyle = workbook.createCellStyle();
		norStyle.setBorderBottom(7);
		norStyle.setBorderTop(7);
		norStyle.setBorderLeft(7);
		norStyle.setBorderRight(7);
		HSSFCellStyle lastNorStyle = workbook.createCellStyle();
		lastNorStyle.setBorderBottom(2);
		lastNorStyle.setBorderTop(7);
		lastNorStyle.setBorderLeft(7);
		lastNorStyle.setBorderRight(7);
		HSSFCellStyle boldStyle = workbook.createCellStyle();
		boldStyle.setFont(boldFont);
		boldStyle.setBorderBottom(2);
		boldStyle.setBorderTop(2);
		boldStyle.setBorderLeft(7);
		boldStyle.setBorderRight(7);
		HSSFCellStyle rightCellStyle = workbook.createCellStyle();
		rightCellStyle.setAlignment(3);
		byte colNum = 12;
		HSSFRow r = sheet.createRow(0);
		r.setHeight(600);
		HSSFCell c = r.createCell(0);

		for (int estStr = 1; estStr < colNum; ++estStr) {
			r.createCell((short) estStr);
		}

		c.setCellStyle(titleCellStyle);
		c.setCellValue(new HSSFRichTextString(lif.getEnterpriseName()
				+ " 送货对帐单"));
		sheet.addMergedRegion(new Region(0, 0, 0, (short) (colNum - 1)));
		r = sheet.createRow(1);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		String arg47 = "订货单位：" + est.getEstName();
		if (includeSub.equals("1")) {
			arg47 = arg47 + "(包括下属单位)";
		}

		HSSFRichTextString ts = new HSSFRichTextString(arg47);
		ts.applyFont(0, 5, boldFont);
		ts.applyFont(5, ts.length(), normalFont);
		c.setCellValue(ts);
		sheet.addMergedRegion(new Region(1, 0, 1, 3));
		c = r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		ts = new HSSFRichTextString("日期：" + startDate + " 至 " + endDate);
		ts.applyFont(0, 3, boldFont);
		ts.applyFont(3, ts.length(), normalFont);
		c.setCellValue(ts);
		sheet.addMergedRegion(new Region(1, 4, 1, 7));
		c = r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		r.createCell(11);
		ts = new HSSFRichTextString("送货金额：" + model.get("costTotal"));
		ts.applyFont(0, 5, boldFont);
		ts.applyFont(5, ts.length(), normalFont);
		c.setCellValue(ts);
		sheet.addMergedRegion(new Region(1, 8, 1, 11));
		r = sheet.createRow(2);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		ts = new HSSFRichTextString("地址：" + est.getAddress());
		ts.applyFont(0, 3, boldFont);
		ts.applyFont(3, ts.length(), normalFont);
		c.setCellValue(ts);
		sheet.addMergedRegion(new Region(3, 0, 3, 3));
		c = r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		EstablishmentContact contact = (EstablishmentContact) model
				.get("contact");
		String linkStr = "联系人：";
		String phoneStr = "电话：";
		if (contact != null) {
			linkStr = linkStr + contact.getContactName();
			if (contact.getPhone() != null) {
				phoneStr = phoneStr + contact.getPhone() + " ";
			}

			if (contact.getMobile() != null) {
				phoneStr = phoneStr + contact.getMobile();
			}
		}

		ts = new HSSFRichTextString(linkStr);
		ts.applyFont(0, 4, boldFont);
		ts.applyFont(4, ts.length(), normalFont);
		c.setCellValue(ts);
		sheet.addMergedRegion(new Region(3, 4, 3, 7));
		c = r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		r.createCell(11);
		ts = new HSSFRichTextString(phoneStr);
		ts.applyFont(0, 3, boldFont);
		ts.applyFont(3, ts.length(), normalFont);
		c.setCellValue(ts);
		sheet.addMergedRegion(new Region(2, 8, 2, 11));
		r = sheet.createRow(3);
		c = r.createCell(0);

		for (int bi = 1; bi < colNum; ++bi) {
			r.createCell((short) bi);
		}

		c.setCellValue(new HSSFRichTextString(""));
		sheet.addMergedRegion(new Region(3, 0, 3, (short) (colNum - 1)));
		r = sheet.createRow(4);
		c = r.createCell(0);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("日期"));
		c = r.createCell(1);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单号"));
		c = r.createCell(2);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("项目类型"));
		c = r.createCell(3);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("内容"));
		c = r.createCell(4);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("材质"));
		c = r.createCell(5);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("规格"));
		c = r.createCell(6);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单位"));
		c = r.createCell(7);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(8);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单价"));
		c = r.createCell(9);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("金额"));
		c = r.createCell(10);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("制作要求"));
		c = r.createCell(11);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("部门/经手人"));
		long preId = 0L;
		long nextId = 0L;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs;
			if (list.size() > os + 1) {
				objs = (Object[]) list.get(os + 1);
				nextId = Long.parseLong(objs[7].toString());
			} else {
				nextId = 0L;
			}

			objs = (Object[]) list.get(os);
			BusinessBillItem arg48 = (BusinessBillItem) objs[0];
			String estName = (String) objs[1];
			String billCode = (String) objs[2];
			Date billDate = (Date) objs[3];
			BigDecimal amount = (BigDecimal) objs[4];
			BigDecimal cost = (BigDecimal) objs[5];
			String linkman = (String) objs[6];
			long billId = Long.parseLong(objs[7].toString());
			String unit = "";
			String spe = arg48.getSpecification();
			if (arg48.getPriceType().equals("AREA")) {
				unit = "平方米";
				spe = spe + " x "
						+ EncCommonHelper.parseBigDecimal(arg48.getAmount());
			} else if (arg48.getPriceType().equals("LENGTH")) {
				if (arg48.getLengType().equals("M")) {
					unit = "米";
				} else {
					unit = "公分";
				}

				spe = spe + " x "
						+ EncCommonHelper.parseBigDecimal(arg48.getAmount());
			} else if (arg48.getPriceType().equals("AMOUNT")) {
				unit = arg48.getUnit();
			}

			String lmStr = "";
			if (includeSub.equals("1") && !est.getEstName().equals(estName)) {
				lmStr = estName + " ";
			}

			lmStr = lmStr + linkman;
			r = sheet.createRow(os + 5);
			c = r.createCell(0);
			this.setDataStyle(c, billId, nextId, norStyle, lastNorStyle);
			if (preId != billId) {
				c.setCellValue(new HSSFRichTextString(sf.format(billDate)));
			}

			c = r.createCell(1);
			this.setDataStyle(c, billId, nextId, norStyle, lastNorStyle);
			if (preId != billId) {
				c.setCellValue(new HSSFRichTextString(billCode));
			}

			c = r.createCell(2);
			this.setDataStyle(c, billId, nextId, norStyle, lastNorStyle);
			c.setCellValue(new HSSFRichTextString(EncCommonHelper
					.getSimpleObjectName(mtList, arg48.getMakeTypeId())));
			c = r.createCell(3);
			this.setDataStyle(c, billId, nextId, norStyle, lastNorStyle);
			c.setCellValue(new HSSFRichTextString(arg48.getFinishedContent()));
			c = r.createCell(4);
			this.setDataStyle(c, billId, nextId, norStyle, lastNorStyle);
			c.setCellValue(new HSSFRichTextString(arg48.getProName()));
			c = r.createCell(5);
			this.setDataStyle(c, billId, nextId, norStyle, lastNorStyle);
			c.setCellValue(new HSSFRichTextString(spe));
			c = r.createCell(6);
			this.setDataStyle(c, billId, nextId, norStyle, lastNorStyle);
			c.setCellValue(new HSSFRichTextString(unit));
			c = r.createCell(7);
			this.setDataStyle(c, billId, nextId, norStyle, lastNorStyle);
			c.setCellValue(amount.doubleValue());
			c = r.createCell(8);
			this.setDataStyle(c, billId, nextId, norStyle, lastNorStyle);
			c.setCellValue(arg48.getUnitPrice().doubleValue());
			c = r.createCell(9);
			this.setDataStyle(c, billId, nextId, norStyle, lastNorStyle);
			c.setCellValue(cost.doubleValue());
			c = r.createCell(10);
			this.setDataStyle(c, billId, nextId, norStyle, lastNorStyle);
			c.setCellValue(new HSSFRichTextString(arg48.getMakeRequire()));
			c = r.createCell(11);
			this.setDataStyle(c, billId, nextId, norStyle, lastNorStyle);
			if (preId != billId) {
				c.setCellValue(new HSSFRichTextString(lmStr));
			}

			preId = billId;
		}

		sheet.setColumnWidth(0, 3200);
		sheet.setColumnWidth(1, 5000);
		ServletOutputStream arg49 = response.getOutputStream();
		workbook.write(arg49);
		arg49.flush();
		arg49.close();
	}

	private void setDataStyle(HSSFCell c, long id, long nextId,
			HSSFCellStyle norStyle, HSSFCellStyle lastNorStyle) {
		if (id == nextId) {
			c.setCellStyle(norStyle);
		} else {
			c.setCellStyle(lastNorStyle);
		}

	}
}